﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;

namespace dst
{
    class Ele : Exp
    {
        string par;
        int idx;

        private Ele(string s, int i)
        {
            par = s;
            idx = i;

            //保证Zero的Sign=0，Unit的Sign=1
            if (s != "")
                this.sig = Util.GetSign(s + i);
            else
                this.sig = i;
        }

        public static Ele GetEle(string s, int i)
        {
            Ele ele = new Ele(s, i);
            return (Ele)Util.simpTable.Add(ele.Sign, ele);
        }

        protected override Exp Add(Exp a)
        {
            if (this == Exp.Zero)
                return a;
            if (a == Exp.Zero)
                return this;

            if (a is Ele)
                return AddExp<Exp>.GetAdd(this, (Ele)a);
            else
                return a + this;
        }

        protected override Exp Mul(Exp a)
        {
            if (this == Exp.Unit)
                return a;
            if (this == Exp.Zero)
                return Exp.Zero;

            if (a is Ele)
                return MulExp<Exp>.GetMul(this, (Ele)a);
            else
                throw new NotImplementedException();
        }

        public override int CompareTo(Exp other)
        {
            if (other is Ele)
                return this.par.CompareTo(((Ele)other).par) == 0 ? this.idx.CompareTo(((Ele)other).idx) : this.par.CompareTo(((Ele)other).par);
            else
                return -1;
        }

        public override void MakeSubSign()
        {
            this.subSigns.Add(this.par, BigInteger.One << (idx - 1));
        }

        public override string ToString()
        {
            return par + idx;
        }
    }
}
